#!/usr/bin/perl

use lib 'mods';
use sender;
use submitter;

$se=new sender('localhost',1337);
$su=new submitter;
$db=$se->{db}; #don't wanna make new db object :(

our $sleep;

sub sender
{
    my $f=$se->get_flags();
    my @fk=keys %$f;
    if ((scalar @fk)==0)
    {
	$sleep=1;
	return undef;
    }else
    {
	$sleep=0;
    }
    $submitted_flags=$su->submit_flags(\@fk);
    map {$good_flags{$$f{$_}}=1;} @$submitted_flags;

    map {$bad_flags{$_}=1;} values %$f; #all flags in %bad_flags now
    map {delete $bad_flags{$_} if (exists $good_flags{$_});} keys %bad_flags;

    @good_flags=keys %good_flags;
    @bad_flags=keys %bad_flags;

    $db->update_fstatus(0,\@bad_flags);
    $db->update_fstatus(2,\@good_flags);
}

while(1)
{
    sender();
    print ".";
    select(undef,undef,undef,0.25) if ($sleep);
}
